package com.yuexunxinxi.carefree.utils;

import com.aliyun.dysmsapi20170525.Client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 作者: 李咨辰
 * 文件名: CaptchaUtils.java
 * 路径: com.yuexunxinxi.carefree.utils
 * 时间: 2024-12-07 14:39
 * 版本: 1.0
 * 描述: TODO
 **/
@Component
public class CaptchaUtil {
    private static String ACCESS_KEY_ID;
    private static String SECRET_ACCESS_KEY;
    private static String ENDPOINT;
    private static String CODE;
    private static String EMAIL_USER_NAME;
    private static String EMAIL_PASSWORD;
    private static String EMAIL_HOST;
    private static String EMAIL_PORT;

    @Value("${alibaba.accessKeyId}")
    private String GET_ACCESS_KEY_ID;

    @Value("${alibaba.secretAccessKey}")
    private String GET_SECRET_ACCESS_KEY;

    @Value("${alibaba.message.endpoint}")
    private String GET_ENDPOINT;

    @Value("${alibaba.message.code}")
    private String GET_CODE;

    @Value("${email.emailUserName}")
    private String GET_EMAIL_USER_NAME;

    @Value("${email.emailPassword}")
    private String GET_EMAIL_PASSWORD;

    @Value("${email.emailHost}")
    private String GET_EMAIL_HOST;

    @Value("${email.emailPort}")
    private String GET_EMAIL_PORT;


    @PostConstruct
    public void init() {
        ACCESS_KEY_ID = GET_ACCESS_KEY_ID;
        SECRET_ACCESS_KEY = GET_SECRET_ACCESS_KEY;
        ENDPOINT = GET_ENDPOINT;
        CODE = GET_CODE;
        EMAIL_USER_NAME = GET_EMAIL_USER_NAME;
        EMAIL_PASSWORD = GET_EMAIL_PASSWORD;
        EMAIL_HOST = GET_EMAIL_HOST;
        EMAIL_PORT = GET_EMAIL_PORT;
    }

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     *
     * @return Client
     */
    private static Client createClient() {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(SECRET_ACCESS_KEY);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = ENDPOINT;
        try {
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String phone(String phone, String captcha) {
        Client client = CaptchaUtil.createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("悦讯信息技术")
                .setTemplateCode(CODE)
                .setTemplateParam("{\"code\":\"" + captcha + "\"}")
                .setSmsUpExtendCode("");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        try {
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return captcha;
    }

    public static String phone(String phone) {
        return CaptchaUtil.phone(phone, String.valueOf((int) ((Math.random() * 900000) + 100000)));
    }

    public static String mail(String email, String captcha) {
        EmailUtil emailUtil = new EmailUtil(EMAIL_USER_NAME, EMAIL_PASSWORD, EMAIL_HOST, EMAIL_PORT);
        String emailText = "<div style='max-width:600px;margin:auto;background-color:#ffffff;padding:20px;border-radius:" +
                "8px;'>" +
                "<p style='font-size:16px;color:#666666;'>尊敬的用户，您申请的验证码为" +
                captcha +
                "，验证码15分钟内有效，如果该验证码不是您本人申请，请忽略此信息！</p>" +
                "<div style='margin-top:20px;font-size:12px;color:#aaaaaa;'><p>此邮件由系统自动生成，请勿回复。</p></div>";
        emailUtil.sendAMessage("问题无忧", email, "请查收您的验证码", emailText, true, "to");
        return captcha;
    }

    public static String mail(String email) {
        return CaptchaUtil.mail(email, String.valueOf((int) ((Math.random() * 900000) + 100000)));
    }

}
